package com.itheima.d05_duotai_use;












/**
 * 目标：理解多态的优点
 *     优点1：写出耦合度低，扩展性强，灵活度高的代码（感受不到）
 *     优点2：编写出功能更强大的方法
 *           形参选择父类，实参接收所有子类
 */
public class Test {


    /**
     * 定义喂养方法，喂养所有人
     */
    public  static void feed(Person p){
        p.eatFood();


        //判断当前喂养的宠物是否是猫，如果是，喂猫条
        if(p instanceof Student){
            Student s = (Student) p;
            s.study();
        }else{
            Teacher t = (Teacher) p;
            t.teach();
        }

    }


    /**
     * 定义游泳比赛的方法
     */
    public static void  swimGame(Person s){
        System.out.println(s.getName() + "开始游泳");
        System.out.println("........");
        System.out.println(s.getName() + "结束游泳");
    }



    public static void main(String[] args) {
        //学生参赛
        swimGame(new Student("小王", 18, "男"));
        feed(new Student("小王", 18, "男"));

        System.out.println();

        //老师赛惨
        swimGame(new Teacher("橘梨纱", 35, "女"));
        feed(new Teacher("橘梨纱", 35, "女"));

        System.out.println();

        //打工人参赛
        swimGame(new Worker("李磊", 33, "男"));
        feed(new Worker("李磊", 33, "男"));


        System.out.println();

        //1.多态的局限性：父类无法调用子类特有方法
        Person p1 = new Student("小王", 18, "男");

        //2.进行强制类型转换： 子 =（子） 父
        if(p1 instanceof  Student){
            Student s1 = (Student) p1;
            s1.study();
        }


        //3.注意：转换子类类型要和原来的类型一致
        Person p2 = new Teacher("小王", 18, "男");
        if(p2 instanceof  Teacher){
            Teacher t1 = (Teacher) p2;
            t1.teach();
        }

    }
}
